#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System;
using System.Net.Mime;

namespace AbstractClass.Extensions
{
    /// <summary>
    ///   Provides a set of extension methods for <see cref="ContentType"/>.
    /// </summary>
    public static class ContentTypeExtensions
    {
        #region | Code Taken from Mannex Library |

        /// <summary>
        ///   Determines whether content type is plain text.
        /// </summary>
        /// <param name = "contentType">Type of the content.</param>
        /// <returns>
        ///   <c>true</c> if the specified content type is plain text; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsPlainText(this ContentType contentType)
        {
            contentType.ThrowIfNull("contentType");
            return EqualsOrdinalIgnoreCase(MediaTypeNames.Text.Plain, contentType.MediaType);
        }

        /// <summary>
        ///   Determines whether content media type is text.
        /// </summary>
        /// <param name = "contentType">Type of the content.</param>
        /// <returns>
        ///   <c>true</c> if the specified content type is text; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsText(this ContentType contentType)
        {
            contentType.ThrowIfNull("contentType");
            return EqualsOrdinalIgnoreCase("text", GetMediaBaseType(contentType));
        }

        /// <summary>
        ///   Determines whether content type identifies an HTML document.
        /// </summary>
        /// <param name = "contentType">Type of the content.</param>
        /// <returns>
        ///   <c>true</c> if the specified content type is HTML; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsHtml(this ContentType contentType)
        {
            contentType.ThrowIfNull("contentType");
            return EqualsOrdinalIgnoreCase(MediaTypeNames.Text.Html, contentType.MediaType);
        }

        /// <summary>
        ///   Determines whether content media type identifies an image.
        /// </summary>
        /// <param name = "contentType">Type of the content.</param>
        /// <returns>
        ///   <c>true</c> if the specified content type is image; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsImage(this ContentType contentType)
        {
            contentType.ThrowIfNull("contentType");
            return EqualsOrdinalIgnoreCase("image", GetMediaBaseType(contentType));
        }

        /// <summary>
        ///   Gets the base media of the content type, e.g. text from text/plain.
        /// </summary>
        /// <param name = "contentType">Type of the content.</param>
        /// <returns>
        /// The base media of the specified content type.
        /// </returns>
        public static string GetMediaBaseType(this ContentType contentType)
        {
            contentType.ThrowIfNull("contentType");
            string mediaType = contentType.MediaType;
            return mediaType.Substring(0, mediaType.IndexOf('/'));
        }

        /// <summary>
        ///   Gets the media sub-type of the content type, e.g. plain from text/plain.
        /// </summary>
        /// <param name = "contentType">Type of the content.</param>
        /// <returns>
        /// The media sub-type of the specified content type.
        /// </returns>
        public static string GetMediaSubType(this ContentType contentType)
        {
            contentType.ThrowIfNull("contentType");
            string mediaType = contentType.MediaType;
            return mediaType.Substring(mediaType.IndexOf('/') + 1);
        }

        private static bool EqualsOrdinalIgnoreCase(string left, string right)
        {
            return left.Equals(right, StringComparison.OrdinalIgnoreCase);
        }

        #endregion
    }
}